package com.app.micaihu.listener;

import android.content.Intent;
import android.text.TextUtils;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.db.UserDBDAO;
import com.app.micaihu.db.UserSQLiteOpenHelper;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.receiver.NoticeReceiver;
import com.app.utils.biz.Biz;

/**
 * 自定义的网络请求监听，配合HttpUtils使用
 * Created by linkai on 2016/7/18.
 */
public abstract class HttpListener<T> {

    /**
     * vollery的成功回调
     */
    private Response.Listener<T> successListener = new Response.Listener<T>() {

        @Override
        public void onResponse(T response) {
            String code = null;
            try {
                code = ((DataBean) response).getCode();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (TextUtils.equals(UserInforManager.TOKEN_ERROR, code)) {//token有误,做下线操作;要不要清空本地呢?
//                AppApplication.getInstance().sendBroadcast(new Intent(NoticeReceiver.TOKENACTION));
                AppApplication.getInstance().sendOrderedBroadcast(new Intent(NoticeReceiver.TOKENACTION), null);
                UserInforManager.getInstance().setUserInfor(null);
                Biz.getInstance().setUserInfo("");
                Biz.getInstance().setUserTask("");

            }
            onSuccess(response);
        }
    };

    /**
     * vollery的失败回调
     */
    private Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            onError(error);
        }
    };

    public void onStart() {
    }

    public abstract void onError(VolleyError error);

    public abstract void onSuccess(T response);

    public Response.Listener<T> getSucesListener() {

        return successListener;
    }

    public Response.ErrorListener getErrorListener() {
        return errorListener;
    }

}
